﻿Shader "Unlit/003"
{
   Properties
    {
        _Diffuse("Diffuse", Color) = (1,1,1,1)
        _Specular("Specular", Color) = (1,1,1,1)
        _Gloss("Gloss", Range(1,256)) = 5
        _MainTex("MainTex",2D) = "white"{}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            
            //只有在CGROGRAM内再次定义一个与属性块内名字与类型相同的变量，属性块对应的变量才能起作用
           fixed4 _Diffuse;
           fixed4 _Specular;
           float _Gloss;
           sampler2D _MainTex;
           float4 _MainTex_ST;
         
            struct v2f
            {
                float4 vertex : SV_POSITION;
                fixed3 worldNormal : TEXCOORD0;
                float3 worldVertex : TEXCOORD1;
                float2 uv:TEXCOORD2;
            };

         
            v2f vert (appdata_full v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                //fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                //fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                //fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLight));
                
                //fixed3 reflectLightDir = normalize(reflect(-worldLight, worldNormal));
                //fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - UnityObjectToWorldDir(v.vertex).xyz);
                
                //fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(reflectLightDir,viewDir)),_Gloss);
               //o.color = diffuse + ambient + specular;
                o.worldVertex = mul(unity_ObjectToWorld, v.vertex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
                
                //在进行发现贴图的时候 需要使用切线空间下的光的向量 以及 观察向量
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
            
                fixed3 albedo = tex2D(_MainTex, i.uv).rgb;
                //漫反射
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * (dot(i.worldNormal, worldLight) * 0.5 + 0.5);
                
                //高光反射
                fixed3 reflectDir = normalize(reflect(-worldLight, i.worldNormal));
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldVertex.xyz);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);
                
                fixed3 color = ambient + diffuse + specular;
                return fixed4(color,1);
            }
            ENDCG
        }
    }
}
